/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SISAC.DACEA.DA;

import SISAC.DACEA.DS.Conexion;
import SISAC.DACEA.ENTITY.Salon;
import SISAC.DACEA.ENTITY.Tutor;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;

/**
 *
 * @author Abel
 */
public class SalonDA {

    public synchronized void crearSalon(Salon oSalon) {
        Connection cn = null;
        CallableStatement call = null;

        try {
            String sql = "{CALL crearSalon(?,?,?,?,?,?)}";

            cn = new Conexion().getConnection();
            call = cn.prepareCall(sql);

            call.registerOutParameter(1, Types.INTEGER);
            call.setInt(2, oSalon.getCodigoTutor());
            call.setString(3, oSalon.getNombreSalon());
            call.setInt(4, oSalon.getCapacidad());
            call.setInt(5, oSalon.getArea());
            call.setInt(6, oSalon.getCantidadActual());

            call.executeUpdate();

        } catch (Exception e) {
            System.out.println("ErrorDA" + e);
        }

    }

    public synchronized ArrayList<Tutor> listarTutor() {
        Connection cn = null;
        CallableStatement call = null;
        ResultSet rs = null;
        ArrayList<Tutor> lstTutor = new ArrayList<Tutor>();
        try {
            String sql = "{CALL listarTutor()}";

            cn = new Conexion().getConnection();
            call = cn.prepareCall(sql);
            rs = call.executeQuery();

            while (rs.next()) {
                Tutor oTutor = new Tutor();
                oTutor.setCodigoTutor(rs.getInt(1));
                oTutor.setNombre(rs.getString(2));
                oTutor.setApePaterno(rs.getString(3));
                oTutor.setApeMaterno(rs.getString(4));

                lstTutor.add(oTutor);
            }

        } catch (Exception e) {
            System.out.println("ErrorDA" + e);
        }
        return lstTutor;

    }

    public synchronized boolean actualizarEstadoTutor(Tutor oTutor) {
        Connection cn = null;
        CallableStatement call = null;

        try {
            String sql = "{CALL actualizarEstadoTutor(?,?)}";

            cn = new Conexion().getConnection();
            call = cn.prepareCall(sql);

            call.setInt(1, oTutor.getCodigoTutor());
            call.setBoolean(2, oTutor.getEstado());


            call.executeUpdate();
            return true;
        } catch (Exception e) {
            System.out.println("ErrorDA" + e);
            return false;
        }

    }

    public synchronized ArrayList<Salon> obtenerCodigoSalon(Salon oSalon) {
        Connection cn = null;
        CallableStatement call = null;
        ResultSet rs = null;
        ArrayList<Salon> listarCodSalon = new ArrayList<Salon>();
        try {
            String sql = "{CALL obtenerCodigoSalon(?)}";

            cn = new Conexion().getConnection();
            call = cn.prepareCall(sql);
            call.setInt(1,oSalon.getArea());
            
            rs = call.executeQuery();

            while (rs.next()) {
                Salon oSalon1 = new Salon();
                oSalon1.setCodigoSalon(rs.getInt(1));
                oSalon1.setNombreSalon(rs.getString(2));
                listarCodSalon.add(oSalon1);
            }
        } catch (Exception e) {
            System.out.println("ErrorDA" + e);
        }
        return listarCodSalon;
    }
    public synchronized void actualizarCantidadMatriculados(Salon oSalon) {
        Connection cn = null;
        CallableStatement call = null;

        try {
            String sql = "{CALL actualizarCantidadMatriculados(?)}";

            cn = new Conexion().getConnection();
            call = cn.prepareCall(sql);

            call.setInt(1, oSalon.getCodigoSalon());
            


            call.executeUpdate();
           
        } catch (Exception e) {
            System.out.println("ErrorDA en actualizar cant de alumnos en salon" + e);
            
        }
    }
    
     public synchronized ArrayList<Salon> cargarArea(Salon oSalon) {
        Connection cn = null;
        CallableStatement call = null;
        ResultSet rs = null;
        ArrayList<Salon> listarArea = new ArrayList<Salon>();
        try {
            String sql = "{CALL cargarArea(?)}";

            cn = new Conexion().getConnection();
            call = cn.prepareCall(sql);
            call.setInt(1,oSalon.getCodigoSalon());
            
            rs = call.executeQuery();

            while (rs.next()) {
                Salon oSalon1 = new Salon();
                oSalon1.setArea(rs.getInt(1));
                listarArea.add(oSalon1);
            }
        } catch (Exception e) {
            System.out.println("ErrorDA" + e);
        }
        return listarArea;
    }
}
